import { WidgetStyle, WidgetType, WidgetOption, WidgetData, RawField } from '@/types'

import WidgetText from './WidgetText.vue'

import WidgetInput from './WidgetInput.vue'
import InputSetting from './InputSetting.vue'
import InputFill from './InputFill.vue'
import {
  getInputTextTips,
  getTextareaTextTips,
  initTextData,
  rawTextCreator,
  rawTextGetter,
  usingValidate
} from './utils'

export * from './utils'

/** 单行文本 */
export default {
  key: 'input',
  name: '单行文本',
  type: WidgetType.INPUT,
  style: WidgetStyle.FILL,
  icon: 'fa-icon-widget-input',
  width: 160,
  height: 30,
  minWidth: 20,
  minHeight: 14,
  resizable: true,
  component: WidgetText,
  settingComponent: InputSetting,
  fillingComponent: InputFill,
  componentExtend: WidgetInput,

  /** 控件初始化 */
  init: initTextData,

  usingValidate,

  labelTips(widget) {
    // 设置模式显示可输入长度
    if (!widget.isSet) return ''

    if (widget.type === WidgetType.INPUT) {
      return getInputTextTips(widget)
    } else if (widget.type === WidgetType.TEXTAREA) {
      return getTextareaTextTips(widget)
    }

    return ''
  },

  rawWidgetCreator(widgetData, data) {
    const { isDecimalLimit: numberFixedLimit, numberFormat: numberFixed, thousands, dateFormat } = data

    rawTextCreator(widgetData, data)

    return Object.assign(widgetData, {
      numberFixedLimit,
      numberFixed,
      thousands,
      dateFormat
    } as WidgetData)
  },

  rawFieldGetter(widget, data) {
    const { numberFixedLimit: isDecimalLimit, numberFixed: numberFormat, thousands, dateFormat } = widget

    rawTextGetter(widget, data)

    return Object.assign(data, {
      isDecimalLimit,
      numberFormat,
      thousands,
      dateFormat
    } as RawField)
  }
} as WidgetOption
